package com.ming.multifunction.controller;
import com.ming.multifunction.common.BaseResponse;
import com.ming.multifunction.common.DeleteRequest;
import com.ming.multifunction.common.ErrorCode;
import com.ming.multifunction.common.ResultUtils;
import com.ming.multifunction.exception.BusinessException;
import com.ming.multifunction.mapper.PostMapper;
import com.ming.multifunction.model.domain.Post;
import com.ming.multifunction.model.domain.User;
import com.ming.multifunction.model.dto.PostQuery;
import com.ming.multifunction.model.dto.PublishPostRequest;
import com.ming.multifunction.model.vo.PostUserVO;
import com.ming.multifunction.service.UserService;
import com.ming.multifunction.service.impl.PostServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帖子接口
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @Resource
    private PostMapper postMapper;

    @Resource
    private PostServiceImpl postService;

    @Resource
    private UserService userService;

    /**
     * 展示列表
     * @param postQuery
     * @param request
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<PostUserVO>> getPostList(PostQuery postQuery, HttpServletRequest request) {

        if (postQuery == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean isAdmin = userService.isAdmin(request);

        List<PostUserVO> post= postService.listPost(postQuery,isAdmin);

        return ResultUtils.success(post);
    }


    /**
     * 获取单个帖子
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<PostUserVO> getPost(Long id){

        if (id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子id不正确");
        }
         PostUserVO postUserVO = postService.getPost(id);
        return ResultUtils.success(postUserVO);
    }


    /**
     * 发布
     * @param publishPostRequest
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public BaseResponse<Long> publishPost(@RequestBody PublishPostRequest publishPostRequest, HttpServletRequest request){

            if (publishPostRequest==null){
                throw new BusinessException(ErrorCode.NULL_ERROR,"内容不能为空");
            }

        User loginUser = userService.getLoginUser(request);
        Post post = new Post();
        BeanUtils.copyProperties(publishPostRequest,post);
        long postId = postService.publishPost(post,loginUser);

        return ResultUtils.success(postId);
    }


    /**
     * 根据关键词查询帖子
     * @param searchText
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<List<PostUserVO>> getPost(@RequestParam(required = false) String searchText){

        if (StringUtils.isBlank(searchText)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"搜索关键词不能为空");
        }
        List<PostUserVO> searchList=postService.searchPost(searchText);

        return ResultUtils.success(searchList);
    }

    /**
     * 删除帖子
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deletePost(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {

        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"请求参数错误");
        }

        long postId = deleteRequest.getId();

        if (postId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"帖子Id不正确");
        }

        boolean removeById = postService.removeById(postId);
        if (!removeById) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return ResultUtils.success(true);
    }



}
